package com.htjc.integral.exchange.utils;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.model.exchange.EcPartner;

import lombok.extern.slf4j.Slf4j;



/**
 *@author:李宇
 *@data:2014年8月20日   @time:下午4:06:55
 *@Described:CommonTools.java 基本工具类
 */
@Slf4j(topic = "exchangeLogger")
public class CommonTools {

	/**
	 * mongodb用户信息映射Partner实体
	 * @param jsonString
	 * @return EcPartner
	 * @throws Exception 
	 */
 public  EcPartner userInfoJsonToPartner(String jsonString) throws Exception { 
	   EcPartner partner = new EcPartner();
	   try {
		   if(StringUtils.isNotBlank(jsonString)){
		       JSONObject json = JSON.parseObject(jsonString); 	
		       log.error("转换之前用户信息："+json);
		     
            //用户安全信息  如果属性值不一一对应时 再用
		    partner.setPartnerId(json.containsKey("userId")?json.getString("userId"):"");		    
		     partner.setLoginAccount(json.containsKey("loginAccount")?json.getString("loginAccount"):"");
		     partner.setPassword(json.containsKey("password")?json.getString("password"):"");
		     partner.setUserType(json.containsKey("userType")?json.getString("userType"):"");
		     partner.setApplyWay(json.containsKey("applyWay")?json.getString("applyWay"):"");
		     partner.setMobile(json.containsKey("mobile")?json.getString("mobile"):"");
		     partner.setMobileActivationSign(json.containsKey("mobileActivationSign")?json.getString("mobileActivationSign"):"");
		     partner.setEmail(json.containsKey("email")?json.getString("email"):"");
		     partner.setEmailActivationSign(json.containsKey("emailActivationSign")?json.getString("emailActivationSign"):"");
		     partner.setFromIpAddress(json.containsKey("fromIpAddress")?json.getString("fromIpAddress"):"");//来源ip
		     partner.setFromSgProvice(json.containsKey("fromSgProvice")?json.getString("fromSgProvice"):"");
		     partner.setLoginFailTimes(json.containsKey("loginFailTimes")?json.getString("loginFailTimes"):"");
		     partner.setLockType(json.containsKey("lockType")?json.getString("lockType"):"");
		     partner.setUnlockDate(checkDateJson(json,"unlockDate"));//解锁时间   
		     partner.setRegisterTime(checkDateJson(json,"registerTime"));//注册时间
		     partner.setVerifyIdea(json.containsKey("verifyIdea")?json.getString("verifyIdea"):"");//审核意见
		     //partner.setPasswordUpdateTime(json.containsKey("passwordUpdateTime")?json.getDate("passwordUpdateTime"):null);
		     partner.setMemberStatus(json.containsKey("memberStatus")?json.getString("memberStatus"):"");//会员状态;0=初始;1=审核中;2=已审批;3=审批不通过
		     partner.setMarketType(json.containsKey("marketType")?json.getString("marketType"):"");//商城类别
		     
		     //用户基本信息
		     partner.setName(json.containsKey("name")?json.getString("name"):"");
		     partner.setNickname(json.containsKey("nickname")?json.getString("nickname"):"");
		     partner.setHobby(json.containsKey("hobby")?json.getString("hobby"):"");
		     partner.setPhoto(json.containsKey("photo")?json.getString("photo"):"");
		     partner.setPost(json.containsKey("post")?json.getString("post"):""); 	   	    
		     partner.setSex(json.containsKey("sex")?json.getString("sex"):"");
		     partner.setEducation(json.containsKey("education")?json.getString("education"):"");
		     partner.setBirthday(checkDateJson(json,"birthday"));//生日
		     partner.setAddressCity(json.containsKey("addressCity")?json.getString("addressCity"):null);
		     partner.setAddressCountry(json.containsKey("addressCountry")?json.getString("addressCountry"):"");
		     partner.setAddressDetail(json.containsKey("addressDetail")?json.getString("addressDetail"):"");
		     partner.setAddressProvince(json.containsKey("addressProvince")?json.getString("addressProvince"):"");
		     partner.setAddressRegion(json.containsKey("addressRegion")?json.getString("addressRegion"):"");
		     //用户其他信息
		     partner.setSchoolName(json.containsKey("schoolName")?json.getString("schoolName"):"");
		     partner.setSchoolScience(json.containsKey("schoolScience")?json.getString("schoolScience"):"");
		     partner.setSchoolType(json.containsKey("schoolType")?json.getString("schoolType"):"");
		     partner.setMarriage(json.containsKey("marriage")?json.getString("marriage"):"");
		     partner.setIncome(json.containsKey("income")?json.getString("income"):"");
		     partner.setIndustry(json.containsKey("industry")?json.getString("industry"):"");
		     partner.setIdcard(json.containsKey("idcard")?json.getString("idcard"):"");
		     partner.setWorkBegintime(json.containsKey("workBegintime")?json.getString("workBegintime"):"");
		     partner.setWorkInstitution(json.containsKey("workInstitution")?json.getString("workInstitution"):"");
		     
		     //等级积分信息
		     partner.setRankChangeDate(checkDateJson(json,"rankChangeDate"));//等级变更
		     partner.setRankNumber(json.containsKey("rankNumber")?json.getString("rankNumber"):"");//等级
		     partner.setNowIntegral(json.containsKey("nowIntegral")?json.getString("nowIntegral"):"");//当年剩余积分
		     partner.setExperience(json.containsKey("experience")?json.getString("experience"):"");//经验值
		     partner.setIntegralCleartime(checkDateJson(json,"integralCleartime"));
		     log.error("json转Partner用户实体成功，PartnerId:"+partner.getPartnerId()+",loginAccount:"+partner.getLoginAccount());
		     return  partner;
	   }else{
		   log.error("用户信息json为空");
		   return null;
	   }
	} catch (Exception e) {
		 log.error("json转换Partner实体异常",e);
		//return null;
		throw new Exception(e);
	}
}
 

 private Date checkDateJson( JSONObject json,String key) { 
	    Date date = null;
	 if(json.containsKey(key) && json.get(key) !=null &&  json.get(key) !=""){
		    try {
		    	date = json.getDate(key);
			} catch (Exception e) { 
				try {
					String value = json.getString(key);
					if(value.indexOf("/")>-1){
						String[] split = value.split("/");
						if(split[0].length()==2){
							split[0] = "20"+split[0];
						}
						if(split[2].split(":").length==2){
							split[2] +=":00";
						}
						String string =new String();
						for (int i = 0; i < split.length; i++) {
							if(i==split.length-1){
							string +=split[i];
						     }else{
						     string +=split[i]+"-"; 
						     }
						}
					log.info("格式化之前属性{key："+key+",value:"+json.get(key)+"}");
						json.put(key, string);
						date = json.getDate(key);
					}else{
					log.info("格式化日期失败的属性{key："+key+",value:"+json.get(key)+"}");
					}
				} catch (Exception e2) {
					log.error("格式化日期异常！属性{key："+key+",value:"+json.get(key)+"}",e2);
				}
			}
	 }
	return date; 
 }
 
 
}
